﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using OpenSoftStore.BusinessLogic.Member;
using OpenSoftStore.BusinessLogic.Common;
using OpenSoftStore.Client.Common;

namespace OpenSoftStore.Client.UIL.ManageMember
{
    public partial class RemindList : UserControl
    {
        private MemberLogic memberBll;
        private ComboLogic comboBll;
        private TreeLogic treeBll;
        public RemindList()
        {
            InitializeComponent();
            memberBll = new MemberLogic();
            comboBll = new ComboLogic();
            treeBll = new TreeLogic();
            dgvRemind.AutoGenerateColumns = false;
        }

        public void LoadControlData()
        {
            SPACommon.SetDataGridStyle(dgvRemind);
            dgvRemind.DataSource = memberBll.GetRemindView(" where R.NextRemindTime between #" + DateTime.Now.Date + "# and #" + DateTime.Now.Date.AddDays(1) + "#");

            cmbContact.DisplayMember = "DisplayText";
            cmbContact.ValueMember = "Value1";
            cmbContact.DataSource = comboBll.GetComboByBuzType("RemindState");
        }

        public void btnSearch_Click(object sender, EventArgs e)
        {
            string treeCode = MemberList.Instance.tvRemind.SelectedNode.Tag.ToString();
            var from = dtpFrom.Value.Date;
            var to = dtpTo.Value.Date.AddDays(1);
            string state = cmbContact.Text;
            string queryStr = txtQuery.Text;
            string whereStr = string.Format(" where T.Code like '{0}%' and  R.NextRemindTime between #{1}# and #{2}#  and R.State='{3}'" +
            " and (M.CardId like '{4}%' or M.Name like '{4}%' or M.TelNum like '{4}%')", treeCode, from, to, state, queryStr);
            dgvRemind.DataSource = memberBll.GetRemindView(whereStr);
        }

        public void GetRemindList(string whereStr = "")
        {
            dgvRemind.DataSource = null;
            dgvRemind.DataSource = memberBll.GetRemindView(whereStr);
        }

        private void btnSearch_KeyUp(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                btnSearch_Click(null, null);
            }
        }

        private void dgvRemind_CellDoubleClick(object sender, DataGridViewCellEventArgs e)
        {
            RemindAddWindow preorderAddWindow = new RemindAddWindow(null);
            preorderAddWindow.ShowDialog();
        }
    }
}
